<?php
/**
 * 模块管理
 */

namespace app\admin\controller;

use think\Controller;
use app\common\model\TplModuleField;

class Module extends Controller
{
    //模块列表
    public function index()
    {

        $tplModel = model("TplModule");

        $list = $tplModel->order("create_time desc")->select();

        $this->assign("list",$list);
        return $this->fetch();
    }

    /**
     * 添加模块
     * @return mixed
     */
    public function add(){
        if (request()->isAjax()){
            try{
                $module_name = input('module_name');
                $module_tpl_url = input('module_tpl_url');

                $tplModel = model("TplModule");
                $data = [
                    'module_name'=>$module_name,
                    'module_tpl_url'=>$module_tpl_url,
                    'create_time'=>time()
                ];

                $tplModel->insert($data);
                return json([
                    'code' => 1,
                    'msg'  => "成功",
                    'data' => '',
                    'url'  => '',
                    'wait' => '',
                ]);
            }catch (\Exception $e){
                //return $this->error($e->getMessage().$e->getLine().$e->getFile());
                return json([
                    'code' => 0,
                    'msg'  => $e->getMessage().$e->getLine().$e->getFile(),
                    'data' => '',
                    'url'  => '',
                    'wait' => '',
                ]);
            }

        }else{
            return $this->fetch();
        }

    }

    /**
     * 修改模块
     * @return mixed
     */
    public function edit(){
        return $this->fetch();
    }

    public function fieldindex(){
        return $this->fetch();
    }

    public function fieldadd($module_id){

        if (request()->isAjax()){
            try{
                $data = input();
                $tplModel = new TplModuleField();
                $isHas = $tplModel->where(['module_id'=>$data['module_id'],'name'=>$data['name']])->find();
                if($isHas){
                    exception("此字段名已存在此模板，请换个字段名");
                }

                $tplModel->insert($data);
                return json([
                    'code' => 1,
                    'msg'  => "succ",
                    'data' => '',
                    'url'  => '',
                    'wait' => '',
                ]);
            }catch (\Exception $e){

                return json([
                    'code' => 0,
                    'msg'  => $e->getMessage().$e->getLine().$e->getFile(),
                    'data' => '',
                    'url'  => '',
                    'wait' => '',
                ]);
        }


        }else{
            $this->assign("module_id",$module_id);
            return $this->fetch();
        }


    }
}